<?php
/**
 * Created by PhpStorm OF Administrator.
 * User: YUYI
 * Date: 2018/9/3 0003
 * Time: 下午 5:35
 * Administrator
 */

namespace app\swoole\entity;


use app\common\model\Users;

class SendMessage
{
    /**
     * 来自谁  用户ID
     * @var int
     */
    protected $from;
    /**
     * 用户昵称
     * @var string
     */
    protected $nickname;
    /**
     * 用户性别  0保密  1男  2女
     * @var int
     */
    protected $sex;
    /**
     * 消息类型
     * @var int 0初始化  1登录通知  2离线通知  3普通消息
     */
    protected $type;
    /**
     * 消息内容
     * @var string|array
     */
    protected $data;
    /**
     * 初始化时传出数据
     * @var array
     */
    protected $onlines = [];
    /**
     * @var
     */
    protected $serv;

    /**
     * @return int
     */
    public function getFrom()
    {
        return $this->from;
    }

    /**
     * @param int $from
     */
    public function setFrom($from)
    {
        $this->from = $from;
    }

    /**
     * @return string
     */
    public function getNickname()
    {
        return $this->nickname;
    }

    /**
     * @param string $nickname
     */
    public function setNickname($nickname)
    {
        $this->nickname = $nickname;
    }

    /**
     * @return int
     */
    public function getSex()
    {
        return $this->sex;
    }

    /**
     * @param int $sex
     */
    public function setSex($sex)
    {
        $this->sex = $sex;
    }

    /**
     * @return int
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @param int $type
     */
    public function setType($type)
    {
        $this->type = $type;
    }

    /**
     * @return array|string
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * @param array|string $data
     */
    public function setData($data)
    {
        $this->data = $data;
    }

    /**
     * @return mixed
     */
    public function getServ()
    {
        return $this->serv;
    }

    /**
     * @param mixed $serv
     */
    public function setServ($serv)
    {
        $this->serv = $serv;
    }

    public function getOnlines()
    {
        return $this->onlines;
    }
    /**
     * @param array $onlines
     */
    public function setOnlines($onlines)
    {
        $info = [];
        foreach ($onlines as $online){
            $conInfo = $this->serv->connection_info($online);
            $user = Users::get($conInfo['uid']);
            if (!$user){
                continue;
            }
            $info[] = [
                'nickname'=>$user->nickname,
                'sex'=>$user->sex,
                'id'=>$online,
            ];
        }
        $this->onlines = $info;
    }

    /**
     * SendMessage constructor.
     * @param $serv @swoole_websocket_server
     * @param $from int
     * @param int|null $type 0初始化  1登录通知  2离线通知  3普通消息
     */
    public function __construct($serv,$from,$type = null)
    {
        $conInfo = $serv->connection_info($from);
        $user = Users::get($conInfo['uid']);
        $this->nickname = $user->nickname;
        $this->sex = $user->sex;
        $this->from = $from;
        $this->serv = $serv;
        $this->type = $type;
    }

    /**
     * 将数据转换成字符串
     * @return array
     */
    public function toString()
    {
        return json_encode([
            'from'=>$this->from,
            'nickname'=>$this->nickname,
            'sex'=>$this->sex,
            'onlines'=>$this->onlines,
            'type'=>$this->type,
            'data'=>$this->data
        ]);
    }

    /**
     * @param $to int 发送信息给用户ID
     */
    public function sendMsg($to)
    {
        $this->serv->push($to,$this->toString());
    }
}